mui.init();
(function($) {
  //阻尼系数
  var page = [1,1,1];
  var flag = [false,false,false];
  var type = ["场地","场地布置","达人服务","包车服务","餐饮服务","套餐","所有"];
  var deceleration = mui.os.ios ? 0.003 : 0.0009;


  $('.mui-scroll-wrapper').scroll({
    bounce: false,
    indicators: true, //是否显示滚动条
    deceleration: deceleration
  });
  $.ready(function() {
    //循环初始化所有下拉刷新，上拉加载。
    $.each(document.querySelectorAll('.mui-slider-group .mui-scroll'), function(index, pullRefreshEl) {

      $(pullRefreshEl).pullToRefresh({
        down: {
          height: 200,
          auto:true,
          callback: function() {
            getData(1,index);
            // console.log(self.getAtrribute("id")+"--");
            // ul.insertBefore(createFragment(ul, index, 10, true), ul.firstChild);
            self.endPullDownToRefresh();
          }
        },
        up: {
          auto:true,
          callback: function() {
            var self = this;
            getData(index);
            console.log(flag);
            self.endPullUpToRefresh(flag[index]);
          }
        }
      });
    });
  });

  // getData(1,0);
function getData(index) {
    //  console.log(associate);
    var url,tpl;
    switch (index) {
      case 0:
        url = "/coup/list/" + page[index] + "?pageSize=10&isUser=true&isOverdue=false&couponStatus=0";
        tpl = "noUse";
        break;
      case 1:
        url = "/coup/list/" + page[index] + "?pageSize=10&isUser=true&couponStatus=1";
        tpl ="used";
        break;
      case 2:
        url = "/coup/list/" + page[index] + "?pageSize=10&isUser=true&isOverdue=true";
        tpl = "overdue";
        break;
      default:
    }
    page[index]++;
    $.ajax(url, {
      type: 'get',
      async: false,
      success: function(res) {
        if(res.result.length<=0){
          flag[index] = true;
        }
        for(var i=0;i<res.result.length;i++){
          res.result[i].type=type[res.result[i].productType];
          res.result[i].startTime =  new Date(res.result[i].startTime).format('yyyy:MM:dd');
          res.result[i].endTime =  new Date(res.result[i].endTime).format('yyyy:MM:dd');
        }
        var allTpl = template(tpl+"Tpl",res);
        //console.log(document.getElementById(elementId)+"----");
        var element = document.getElementById(tpl+"List");
        element.innerHTML = element.innerHTML+allTpl;
      }
    });

  }

})(mui);
